(function() {
  $(function() {
    var err, form, i, shortcut, shortcuts, _i, _len;
    form = $('form.main');
    form.form({
      inline: true,
      fields: {
        loginname: ['minLength[4]', 'regExp[/^[0-9a-zA-Z]+$/]'],
        password: ['minLength[6]'],
        password2: 'match[password]',
        role: 'empty',
        realname: 'empty',
        gender: 'empty',
        birthday: 'empty',
        mobile: ['empty', 'integer[..]'],
        email: ['empty', 'email'],
        dept: 'empty'
      }
    });
    form.find('.dropdown.shortcut').dropdown({
      onChange: function(value, text, $selection) {
        var arr, title_item;
        title_item = $(this).find('select').attr('name') + "_title";
        arr = text.split("—");
        return $("input[name=" + title_item + "]").val(arr[arr.length - 1]);
      }
    });
    shortcuts = form.form('get value', 'shortcuts');
    if (shortcuts) {
      try {
        shortcuts = JSON.parse(shortcuts);
        for (i = _i = 0, _len = shortcuts.length; _i < _len; i = ++_i) {
          shortcut = shortcuts[i];
          $("select[name=shortcut" + (i + 1) + "]").parent('.dropdown').dropdown('set selected', shortcut.href);
          $("input[name=shortcut" + (i + 1) + "_title]").val(shortcut.title);
        }
      } catch (_error) {
        err = _error;
        alert('用户的快捷菜单配置有误，请重新配置!');
      }
    }
    return form.submit(function(evt) {
      var href, items, title, val, _j;
      items = [];
      for (i = _j = 1; _j <= 6; i = ++_j) {
        title = $("input[name=shortcut" + i + "_title]").val();
        href = $("select[name=shortcut" + i + "]").val();
        if (title.length > 0 && href.length > 0) {
          items.push({
            title: title,
            href: href
          });
        }
      }
      $(this).form('set value', 'shortcuts', JSON.stringify(items));
      val = $(this).form('get value', 'password');
      if (val === "it's not a password") {
        return $(this).form('set value', 'password', '');
      }
    });
  });

}).call(this);
